﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    public static AudioManager Instance;
    private float volume=1;
    public AudioSource Bgm;
    public Dictionary<int, AudioSource> audioDic = new Dictionary<int, AudioSource>();
    private void Awake()
    {
        Instance = this;

    }
    public void PlayBGM(AudioClip audioClip, bool loop=true)
    {
        Bgm.Pause();
        Bgm.loop = loop;
        Bgm.clip = audioClip;
        Bgm.Play();
    }

    public void SetBGMVolum(float vo)
    {
        vo = Mathf.Clamp(vo, 0, 1);
        Bgm.volume=vo;
    }
        
         
    public void SetAllVolume(float volume)
    {
        this.volume = Mathf.Clamp(volume,0,1);
        foreach (var item in audioDic.Values)
        {
            item.volume = this.volume;
        }
        SetBGMVolum(volume);
    }

    public void Register(AudioSource audioSource)
    {
        int hashCode = audioSource.GetHashCode();
        if (!audioDic.ContainsKey(hashCode))
        {
            audioDic.Add(hashCode,audioSource);
        }
    }

    public void UnRegister(AudioSource audioSource)
    {
        int hashCode = audioSource.GetHashCode();
        audioDic.Remove(hashCode);
    }
}
